package me.numark.view.checktagview;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import me.numark.R;
import me.numark.model.FeedTag;
import me.numark.view.CircleImageView;

public class TagListView extends FlowLayout implements View.OnClickListener {
     public  int fromType;
     private boolean mIsDeleteMode;  
     private OnTagCheckedChangedListener mOnTagCheckedChangedListener;  
     private OnTagClickListener mOnTagClickListener;  
     private int mTagViewBackgroundResId;  
     private int mTagViewTextColorResId;  
     public final List<FeedTag> mTags = new ArrayList<FeedTag>();
  
     /**  
     * @param context  
     */  
     public TagListView(Context context) {
          super(context);  
          // TODO Auto-generated constructor stub  
          init();  
     }  
  
     /**  
     * @param context  
     * @param attributeSet  
     */  
     public TagListView(Context context, AttributeSet attributeSet) {
          super(context, attributeSet);  
          // TODO Auto-generated constructor stub  
          init();  
     }  
  
     /**  
     * @param context  
     * @param attributeSet  
     * @param defStyle  
     */  
     public TagListView(Context context, AttributeSet attributeSet, int defStyle) {  
          super(context, attributeSet, defStyle);  
          // TODO Auto-generated constructor stub  
          init();  
     }  
  
     @Override  
     public void onClick(View v) {  
          if ((v instanceof TagView)) {
               FeedTag localTag = (FeedTag) v.getTag();
               if (this.mOnTagClickListener != null) {  
                    this.mOnTagClickListener.onTagClick((TagView) v, localTag);  
               }  
          }  
     }  
  
     private void init() {  
  
     }  
  
     private void inflateTagView(final FeedTag t, boolean b) {

          final TagLayoutView localTagView = (TagLayoutView) View.inflate(getContext(),
                    R.layout.tagview_layout, null);

          localTagView.textView = (TextView) localTagView.findViewById(R.id.tag_number);
          localTagView.circleImageView = (CircleImageView)localTagView.findViewById(R.id.avatar);
          localTagView.tagView = (TagView)localTagView.findViewById(R.id.tag);
          if (fromType == 1){
               localTagView.tagView.setText(t.content);
          }else if (fromType ==3){
               localTagView.tagView.setText(t.content);
          } else {
               localTagView.tagView.setText(t.title);
          }
          localTagView.tagView.setTag(t);
          localTagView.setTag(t);



          localTagView.tagView.setChecked(t.self_like);
          localTagView.tagView.setCheckEnable(b);
          /*if (mIsDeleteMode) {
               int k = (int) TypedValue.applyDimension(1, 5.0F, getContext()
                       .getResources().getDisplayMetrics());
               localTagView.tagView.setPadding(localTagView.getPaddingLeft(),
                         localTagView.tagView.getPaddingTop(), k,
                         localTagView.tagView.getPaddingBottom());
               localTagView.tagView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                         R.drawable.forum_tag_close, 0);  
          }*/
         /* if (t.getBackgroundResId() > 0) {
               localTagView.tagView.setBackgroundResource(t.getBackgroundResId());
          }*/

          localTagView.tagView.setOnClickListener(this);
          localTagView.tagView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               public void onCheckedChanged(CompoundButton paramAnonymousCompoundButton, boolean paramAnonymousBoolean) {
                    //t.setChecked(paramAnonymousBoolean);
                    if (TagListView.this.mOnTagCheckedChangedListener != null) {
                         TagListView.this.mOnTagCheckedChangedListener.onTagCheckedChanged(localTagView, t);
                    }
               }
          });
          if (fromType == 0) {
               if ((t.user == null) || TextUtils.isEmpty(t.user.pic)) {
                    Picasso.with(getContext()).load(R.drawable.ic_launcher).into(localTagView.circleImageView);
               } else {
                    Picasso.with(getContext()).load(t.user.pic).into(localTagView.circleImageView);
               }
          }else {
               localTagView.circleImageView.setVisibility(View.GONE);
          }
          if (fromType ==3) {
               localTagView.textView.setVisibility(View.GONE);
          }else{
               if (t.like_num == 0) {
                    localTagView.textView.setText("0");
               } else if(t.like_num > 99){
                    localTagView.tagView.setText("99+");
               } else{
                    localTagView.textView.setText(String.valueOf(t.like_num));
               }
          }
          if (t.self_like){
               localTagView.textView.setTextColor(Color.parseColor("#ffffff"));
          }else {
               localTagView.textView.setTextColor(Color.parseColor("#00b16a"));
          }
          addView(localTagView);  
     }  
  
     public void addTag(int i, String s) {  
          addTag(i, s, false);  
     }  
  
     public void addTag(int i, String s, boolean b) {  
          addTag(new FeedTag(i, s), b);
     }  
  
     public void addTag(FeedTag tag) {
          addTag(tag, false);  
     }  
  
     public void addTag(FeedTag tag, boolean b) {
          mTags.add(tag);  
          inflateTagView(tag, b);  
     }  
  
     public void addTags(List<FeedTag> lists) {
          addTags(lists, false);
     }  
  
     public void addTags(List<FeedTag> lists, boolean b) {
          for (int i = 0; i < lists.size(); i++) {  
               addTag((FeedTag) lists.get(i), b);
          }  
     }

     public void addTags(FeedTag[] feedTags,boolean b){
          for(FeedTag ft : feedTags){
               addTag(ft,b);
          }
     }
  
     public List<FeedTag> getTags() {
          return mTags;  
     }  

     public View getViewByTag(FeedTag tag) {
          return findViewWithTag(tag);  
     }  
  
     public void removeTag(FeedTag tag) {
          mTags.remove(tag);  
          removeView(getViewByTag(tag));  
     }  
  
     public void setDeleteMode(boolean b) {  
          mIsDeleteMode = b;  
     }  
  
     public void setOnTagCheckedChangedListener(  
               OnTagCheckedChangedListener onTagCheckedChangedListener) {  
          mOnTagCheckedChangedListener = onTagCheckedChangedListener;  
     }  
  
     public void setOnTagClickListener(OnTagClickListener onTagClickListener) {  
          mOnTagClickListener = onTagClickListener;  
     }  
  
     public void setTagViewBackgroundRes(int res) {  
          mTagViewBackgroundResId = res;  
     }  
  
     public void setTagViewTextColorRes(int res) {  
          mTagViewTextColorResId = res;  
     }  
  
     public void setTags(List<? extends FeedTag> lists) {
          setTags(lists, false);
     }  
  
     public void setTags(List<? extends FeedTag> lists, boolean b) {
          removeAllViews();  
          mTags.clear();  
          for (int i = 0; i < lists.size(); i++) {  
               addTag((FeedTag) lists.get(i), b);
          }  
     }

     public void setTags(FeedTag[] arrayTags,boolean b){
          removeAllViews();
          mTags.clear();;
          for(FeedTag ft: arrayTags){
               addTag(ft,b);
          }
     }

     public void removeAllTags(){
          removeAllViews();
          for (FeedTag ft : mTags){
               removeTag(ft);
          }
     }

     public abstract interface OnTagCheckedChangedListener {
          public abstract void onTagCheckedChanged(TagLayoutView tagLayoutView, FeedTag tag);
     }  
  
     public abstract interface OnTagClickListener {
          public abstract void onTagClick(TagView tagView, FeedTag tag);
     }  
  
}  